<template>
  <div class="table-page-search-wrapper">
    <a-card :bordered="false" class="card-area">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item
              label="学生来源"
            >
              <a-select :allowClear="true" v-model="queryParam.xslxm">
                <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch
                        :filterOption="filterOption" v-model="queryParam.ssyxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="年级">
              <a-select :allowClear="true" v-model="queryParam.sznj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="学年">
              <a-select :allowClear="true" v-model="queryParam.xn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="学期">
              <a-select :allowClear="true" v-model="queryParam.xq">
                <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="学生类别">
                <a-select :allowClear="true" v-model="queryParam.xslbm">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="是否全日制">
                <a-select allowClear v-model="queryParam.isqrz">
                  <a-select-option v-for="i in TYPE0441" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="汇总方式">
                <a-select v-model="queryParam.hzfs">
                  <a-select-option value="yx">按院系汇总</a-select-option>
                  <a-select-option value="nj">按年级汇总</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="loadData">汇总</a-button>
               <a-button type="primary" @click="exportExcel">导出</a-button>
              <a-button style="margin-left: 8px"
                        @click="() => queryParam = {ssyxm: selectMenu.yx.length === 1 ? queryParam.ssyxm : ''}">重置</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <div class="table-page-search-result">
      <a-alert :message="'总条数：' + data.length" type="info" show-icon />
      <a-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :loading="loading"
        :dataSource="data"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pagination="false"
        :scroll="{x: 1600}"
      >
      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
        <template slot="zrs" slot-scope="text, record">
          <a @click="showStu(record)">{{ text }}</a>
          <!-- <span v-else>{{ text }}</span> -->
        </template>
        <template slot="ybdrs" slot-scope="text, record">
          <a  @click="showStu(record, 'bdztm,02')">{{ text }}</a>
          <!-- <span v-else>{{ text }}</span> -->
        </template>
        <template slot="wbdrs" slot-scope="text, record">
          <a  @click="showStu(record, 'bdztm,01')">{{ text }}</a>
          <!-- <span v-else>{{ text }}</span> -->
        </template>
        <template slot="yzcrs" slot-scope="text, record">
          <a  @click="showStu(record, 'zcztm,02')">{{ text }}</a>
          <!-- <span v-else>{{ text }}</span> -->
        </template>
        <template slot="wzcrs" slot-scope="text, record">
          <a  @click="showStu(record, 'zcztm,01')">{{ text }}</a>
          <!-- <span v-else>{{ text }}</span> -->
        </template>
      </a-table>
    </div>
    <stu-list-Modal ref="stuList" :isRegister="isRegister" :visible="visible" @cancel="visible = false" @ok="visible = false"/>
  </div>
</template>

<script>
  // import moment from 'moment'
  import {STable} from '~'
  import {mapState} from 'vuex'
  import stuListModal from '../components/stuListModal'

  const selectArr = ['TYPE0311', 'TYPE0312', 'TYPE0126', 'TYPE0075', 'TYPE0056', 'TYPE0083', 'TYPE0125', 'TYPE0072', 'TYPE0503', 'TYPE0441']
  export default {
    name: 'TableList',
    components: {
      STable,
      stuListModal
    },
    data() {
      return {
        isRegister: true,
        loading: false,
        data: [],
        // 高级搜索 展开/关闭
        advanced: true,
        // 查询参数
        queryParam: {
          hzfs: 'yx',
          xn: '',
          xq: '',
          xslxm: ''
        },
        postParams: {},
        columns: [],
        // 院系表头
        yxColumns: [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'},
            width: 60,
            fixed: 'left'
          },
          {
            title: '院系名称',
            dataIndex: 'yxmc',
            fixed: 'left'
          },
          {
            title: '学年',
            dataIndex: 'xn'
          },
          {
            title: '学期',
            width: 100,
            dataIndex: 'xqmc'
          },
          {
            title: '总人数',
            width: 100,
            scopedSlots: {customRender: 'zrs'},
            dataIndex: 'zrs'
          },
          {
            title: '已报到人数',
            width: 120,
            scopedSlots: {customRender: 'ybdrs'},
            dataIndex: 'ybdrs'
          },
          {
            title: '未报到人数',
            width: 120,
            scopedSlots: {customRender: 'wbdrs'},
            dataIndex: 'wbdrs'
          },
          {
            title: '报到率',
            width: 100,
            dataIndex: 'bdl'
          },
          {
            title: '已注册人数',
            width: 120,
            scopedSlots: {customRender: 'yzcrs'},
            dataIndex: 'yzcrs'
          },
          {
            title: '未注册人数',
            width: 120,
            scopedSlots: {customRender: 'wzcrs'},
            dataIndex: 'wzcrs'
          },
          {
            title: '注册率',
            width: 100,
            dataIndex: 'zcl'
          },
          {
            title: '欠费人数',
            width: 100,
            dataIndex: 'qfrs'
          },
          {
            title: '欠费率',
            width: 100,
            dataIndex: 'qfl'
          }
        ],
        // 年级表头
        // 表头
        njColumns: [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'},
            width: 60,
            fixed: 'left'
          },
          {
            title: '年级',
            width: 100,
            dataIndex: 'yxmc',
            fixed: 'left'
          },
          {
            title: '学年',
            dataIndex: 'xn'
          },
          {
            title: '学期',
            width: 100,
            dataIndex: 'xqmc'
          },
          {
            title: '总人数',
            width: 100,
            dataIndex: 'zrs'
          },
          {
            title: '已报到人数',
            width: 120,
            dataIndex: 'ybdrs'
          },
          {
            title: '未报到人数',
            width: 120,
            dataIndex: 'wbdrs'
          },
          {
            title: '报到率',
            width: 100,
            dataIndex: 'bdl'
          },
          {
            title: '已注册人数',
            width: 120,
            dataIndex: 'yzcrs'
          },
          {
            title: '未注册人数',
            width: 120,
            dataIndex: 'wzcrs'
          },
          {
            title: '注册率',
            width: 100,
            dataIndex: 'zcl'
          },
          {
            title: '欠费人数',
            width: 100,
            dataIndex: 'qfrs'
          },
          {
            title: '欠费率',
            width: 100,
            dataIndex: 'qfl'
          }
        ],
        selectedRowKeys: [],
        selectedRows: [],

        // custom table alert & rowSelection
        options: {
          alert: {show: true}
        },
        optionAlertShow: true, // 是否显示alert，已选中
        visible: false,
        confirmLoading: false,
        editId: null,
        selectMenu: {
          yx: [], // 院系
          zy: [] // 所属专业
        }
      }
    },
    computed: {
      ...mapState({
        systemInfo: state => state.app.systemInfo,
        TYPE0125: state => state.app['TYPE0125'], // 学期
        TYPE0072: state => state.app['TYPE0072'], // 学年
        TYPE0311: state => state.app['TYPE0311'], // 学业奖学金等级
        TYPE0312: state => state.app['TYPE0312'], // 助研奖学金等级
        TYPE0126: state => state.app['TYPE0126'], // 年级
        TYPE0075: state => state.app['TYPE0075'], // 学生类别
        TYPE0056: state => state.app['TYPE0056'], // 录取类别
        TYPE0083: state => state.app['TYPE0083'], // 入学方式
        TYPE0503: state => state.app['TYPE0503'],
        TYPE0441: state => state.app['TYPE0441'],
      })
    },
    created() {
      this.columns = this.yxColumns
      this.$store.dispatch('app/sysInfo').then(res => {
        this.queryParam.xn = this.systemInfo.schoolYear
        this.queryParam.xq = this.systemInfo.currentSemester
      })
      selectArr.forEach(v => {
        this.$store.dispatch('app/setDictionary', v)
      })
      this.initSelectMenu()
    },
    methods: {
      initSelectMenu() {
        let allRequest = [
          this.$api.base.collegeAll()
        ]
        Promise.all(allRequest).then(res => {
          this.selectMenu = {
            yx: res[0].data
          }
          if (this.selectMenu.yx && this.selectMenu.yx.length == 1) {
            this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
            this.yxChange(this.queryParam.ssyxm)
          }
          this.$nextTick(() => {
            this.loadData()
            this.queryParam.xslxm = '01'
          })
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      audit() {
        this.xh = this.selectedRows.map(v => {
          return v.xh
        }).toString()
        this.$api.student.checkInRegisterTotal(this.queryParam).then(res => {
          this.$message.success('汇总成功')
        })
      },
      yxChange(code) {
        this.$set(this.queryParam, 'sszym', '')
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      },
      filterOption(value, op) {
        return op.componentOptions.children[0].text.indexOf(value) !== -1
      },
      // 加载数据方法 必须为 Promise 对象
      loadData() {
        this.loading = true
        if (this.queryParam && this.queryParam.hzfs === 'nj') {
          this.columns = this.njColumns
        } else {
          this.columns = this.yxColumns
        }
        return this.$api.student.checkInRegisterTotal(this.queryParam).then(res => {
          this.loading = false
          this.data = res.data
        })
      },
      exportExcel() {
        let params = {...this.queryParam}
        delete params.pageNum
        this.$export('/schoolRoll/bdzcxx/exportCheckInRegisterTotal', params)
      },
      showStu(record, valueInkey) {
        if (valueInkey) {
          let arr = valueInkey.split(',')
          this.isRegister = arr[0] === 'bdztm' ? false : true
        } else {
          this.isRegister = true
        }
        this.visible = true
        let {xn, xq} = record
        if(record.yxmc == '合计'){
          xn=this.queryParam.xn
          xq=this.queryParam.xq
        }
        this.$nextTick(() => {
          this.$refs.stuList.params = {
            ssyxm: record.yxm,
            xn,
            xq,
            xslxm: this.queryParam.xslxm,
            sznj:this.queryParam.sznj,
            xslbm:this.queryParam.xslbm,
            isqrz:this.queryParam.isqrz
          }
          if (valueInkey) {
            let arr = valueInkey.split(',')
            this.$refs.stuList.params[arr[0]] = arr[1]
          }
          this.$refs.stuList.$refs.table.refresh(true)
        })
      }
    }
  }
</script>
